<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="选择产品" prop="ProductID">
        <el-select v-model="form.ProductID" @change="seleProduct">
          <el-option label="全部" :value="0" />
          <el-option
            v-for="(item, index) in allList.productList"
            :key="index"
            :label="item.ProductName"
            :value="item.ProductID"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="选择服务类型" prop="WorkTaskClassID">
        <el-select v-model="form.WorkTaskClassID" @change="seleProduct">
          <el-option
            v-for="(item, index) in allList.WorkTaskClass"
            :key="index"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="任务名称" required prop="WorkTaskName">
        <el-input v-model="form.WorkTaskName" />
      </el-form-item>
      <el-form-item label="指派任务人" required prop="AssignOrderSetID">
        <el-select v-model="form.AssignOrderSetID" @change="seleProduct">
          <el-option label="不限" :value="0" />
          <el-option
            v-for="(item, index) in allList.AssignOrderSetList"
            :key="index"
            :label="item.AssignOrderTargetName"
            :value="item.AssignOrderSetID"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务单位" required prop="WorkTaskUnit">
        <el-input v-model="form.WorkTaskUnit" />
      </el-form-item>
      <el-form-item label="提成金额" required prop="TichengPrice">
        <el-input-number v-model="form.TichengPrice" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { WorkTaskSetEdit } from '@/api/systemsetBusinessProduct'

  export default defineComponent({
    name: 'TaskSettingEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        allList: {},
        formRef: null,
        form: {},
        rules: {
          WorkTaskName: [
            { required: true, trigger: 'blur', message: '请输入任务名称' },
          ],
          AssignOrderSetID: [
            { required: true, trigger: 'blur', message: '请输入指派人员' },
          ],
          WorkTaskUnit: [
            { required: true, trigger: 'blur', message: '请输入任务单位' },
          ],
          TichengPrice: [
            { required: true, trigger: 'blur', message: '请填写提成金额' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        BusinessClassOptions: [],
        ProductClassOptions: [],
      })

      const showEdit = (row, list, index) => {
        state.allList = list
        if (index == 1) {
          state.title = '编辑任务'
          state.form = Object.assign({}, row)
        } else {
          state.form = Object.assign({}, row)
          console.log(state.form, 'state.form')
          state.form.ProductID = 0
          state.form.WorkTaskClassID = 0
          state.form.TichengPrice = 0
          state.title = '添加任务'
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.dialogFormVisible = false
      }
      const save = () => {
        console.log(state.form, 'state.form')
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await WorkTaskSetEdit(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      // 选择产品
      const seleProduct = (item) => {
        console.log(state.form, 'state.formstate.form')
        console.log(item, '选择产品')
      }

      return {
        ...toRefs(state),
        seleProduct,
        showEdit,
        close,
        save,
      }
    },
  })
</script>
